<template>
    <div>
      <!-- vent中的下拉刷新组件  van-pull-refresh-->
      <van-pull-refresh
        :success-text="refreshSuccessText"
        :success-duration="1500"
        v-model="isRefreshLoading"
        @refresh="onRefresh">
          <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-cell
          v-for="(e,i) in list"
          :key="i"
          :title="e.title"
          />
        </van-list>
      </van-pull-refresh>
    </div>
</template>

<script>
import { getAticles } from '@/api/article'
export default {
  name: 'ArticleList',
  components: {},
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      list: [],
      isRefreshLoading: false, // 下拉loding结束
      refreshSuccessText: ''// 下拉结束后的提示
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 进入页面加载新闻
    async onLoad () {
      try {
        const { data: res } = await getAticles({ channel_id: this.channel.id, timestamp: Date.now() }) // 需要传的的参数
        console.log(res)
        const { results } = res.data
        this.list.push(...results)
        this.loading = false
        if (results.length) {
          // 更新获取下一页数据的时间戳
          this.timestamp = res.data.pre_timestamp
        } else {
          // 没有数据了，设置加载状态结束，不再触发上拉加载更多了
          this.finished = true
        }
      } catch (error) {
        console.log(error)
        this.loading = false // 关闭 loading 效果
        this.error = true // 开启错误提示
      }
    },
    // 下拉刷新页面
    async onRefresh () {
      try {
        const { data: res } = await getAticles({ channel_id: this.channel.id, timestamp: Date.now() })
        console.log(res)
        this.list.unshift(...res.data.results)
        this.isRefreshLoading = false
        this.refreshSuccessText = `加载成功,更新了${res.data.results.length}条数据`
      } catch (error) {
        this.isRefreshLoading = false // 关闭下拉刷新的 loading 状态
        this.$toast('刷新失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 2;
  width: 100%;
}</style>
